/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.hiv.common;

import com.hiv.common.enums.ErrorCode;
import com.hiv.common.service.RedisService;
import com.hiv.common.utils.AssertUtil;
import com.hiv.mapper.AdminMapper;
import com.hiv.mapper.RoleMapper;
import com.hiv.mapper.domain.Admin;
import com.hiv.mapper.domain.Role;
import com.hiv.service.HospitalService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author zhanghong
 * @version BaseController.java, v 0.1 2022/3/21 09:56 下午 BaseController Exp $$
 */
@Component
public class BaseController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private HospitalService hospitalService;

    /**
     * 小程序用户端-登录校验
     *
     * @param request
     */
    public <T> T validUserLoginAndGet(HttpServletRequest request, Class<T> clazz) {
        String token = request.getHeader(Constants.TOKEN);
        AssertUtil.assertNotNull(token, ErrorCode.NOT_LOGIN);
        T value = redisService.getKey(String.format(Constants.LOGIN_USER_RDB_PREFIX, token), clazz);
        AssertUtil.assertUserNotNull(value, ErrorCode.NOT_LOGIN);
        Admin admin = (Admin) value;
        admin = adminMapper.selectById(admin.getId());
        if (admin != null) {
            Role role = roleMapper.findByAdminId(admin.getId());
            AssertUtil.assertNotNull(role, "非法的用户,角色不存在,联系管理员");
            admin.setRoleId(role.getId());
            admin.setPlatform(role.getPlatform());
            admin.setWechatRoleType(role.getWechatRoleType());
            if (Objects.equals(admin.getPlatform(), 1)) {
                admin.setHospitalName(hospitalService.getHospitalName(admin.getHospitalId()));
            }
        }
        return (T) admin;
    }

    /**
     * 小程序用户端-获取用户信息
     *
     * @param request
     */
    public <T> T getLoginUser(HttpServletRequest request, Class<T> clazz) {
        String token = request.getHeader(Constants.TOKEN);
        if (StringUtils.isBlank(token)) {
            return null;
        }
        T value = redisService.getKey(String.format(Constants.LOGIN_USER_RDB_PREFIX, token), clazz);
        return value;
    }

    /**
     * 小程序用户端-退出登陆
     *
     * @param request
     * @return
     */
    public boolean logoutUser(HttpServletRequest request) {
        String token = request.getHeader(Constants.TOKEN);
        AssertUtil.assertNotNull(token, ErrorCode.NOT_LOGIN);
        String key = redisService.getKey(String.format(Constants.LOGIN_USER_RDB_PREFIX, token));
        if (StringUtils.isNotBlank(key)) {
            redisService.del(key);
        }
        return true;
    }

    /**
     * 管理端-登录校验
     *
     * @param request
     */
    public Admin validAdminLoginAndGet(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute(Constants.LOGIN_ADMIN);
        AssertUtil.assertAdminNotNull(admin, ErrorCode.NOT_LOGIN);
        AssertUtil.assertTrue(admin.getStatus() == 1, "账号已被禁用，请联系管理员");
        return admin;
    }

}
